
package com.primecodegen.common.DmObjProp;

import com.primecodegen.common.DataModelObject.DataModelObjectDAO;
import com.primecodegen.common.DataModelObject.DataModelObjectTable;
import com.primecodegen.common.ObjectProperties.ObjectPropertiesDAO;
import com.primecodegen.common.ObjectProperties.ObjectPropertiesTable;
import com.primecodegen.util.*;
import com.primecodegen.common.base.BaseBean;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.logging.*;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import javax.servlet.http.HttpSession;

@ManagedBean(name = "dmObjPropBean")
@RequestScoped
public class DmObjPropBean extends BaseBean implements Serializable {

    // *********** Local Variables/Objects ***********
    private static final Logger logger = Logger.getLogger(DmObjPropBean.class.getName());
    private int count;
    private int dmObjPropID;
    private String excludedClmn;
    private DmObjPropTable selectedDmObjProp;
    HttpSession session;
    private DmObjPropDAO dmObjPropDAO;
    private ArrayList<DmObjPropTable> dmObjPropList;
    private DmObjPropTable dmObjPropSrchCrit;
    private DmObjPropTable dmObjPropUpdate;
    private DmObjPropTable dmObjPropTable;

    FacesContext context;
    ColToOrderBy colToOrderBy;

    // *********** Constructors ***********
    public DmObjPropBean() {
        logger.info(" Constructor DmObjPropBean()");
        excludedClmn="6";
        context = getContext();
        session = getSession();
        dmObjPropDAO = new DmObjPropDAO();
        dmObjPropList = new ArrayList<DmObjPropTable>();
        dmObjPropTable = new DmObjPropTable();
        dmObjPropSrchCrit = (DmObjPropTable) session.getAttribute("sessionDmObjPropSrchCrit");
        dmObjPropUpdate = (DmObjPropTable) session.getAttribute("dmObjPropUpdateSess");
        colToOrderBy = (ColToOrderBy) session.getAttribute("sessionColToOrderBy");

        if (dmObjPropSrchCrit == null) {
            dmObjPropSrchCrit = new DmObjPropTable();
        }

        if (dmObjPropUpdate == null) {
            dmObjPropUpdate = new DmObjPropTable();
        }



        fillDmObjPropList();
    }


    // *********** Setters & Getters ***********
    public String getExcludedClmn() {
        return excludedClmn;
    }

    public void setExcludedClmn(String excludedClmn) {
        this.excludedClmn = excludedClmn;
    }

    public DmObjPropTable getDmObjPropUpdate() {
        dmObjPropUpdate = (DmObjPropTable) session.getAttribute("dmObjPropUpdateSess");
         if (dmObjPropUpdate == null) {
            dmObjPropUpdate = new DmObjPropTable();
        }
        return dmObjPropUpdate;
    }

    public void setDmObjPropUpdate(DmObjPropTable dmObjPropUpdate) {
        session.setAttribute("dmObjPropUpdateSess", dmObjPropUpdate);
        this.dmObjPropUpdate = dmObjPropUpdate;
    }



    public DmObjPropBean(DmObjPropTable dmObjPropTable) {
        logger.info("Constructor DmObjPropBean( DmObjPropTable dmObjPropTable )");
        this.dmObjPropTable = dmObjPropTable;
    }

    public DmObjPropTable getDmObjPropTable() {
        return dmObjPropTable;
    }

    public void setDmObjPropTable(DmObjPropTable dmObjPropTable) {
        this.dmObjPropTable = dmObjPropTable;
    }

    public DmObjPropTable getDmObjPropSrchCrit() {
        return dmObjPropSrchCrit;
    }

    public void setDmObjPropSrchCrit(DmObjPropTable dmObjPropSrchCrit) {
        this.dmObjPropSrchCrit = dmObjPropSrchCrit;
    }

    public DmObjPropTable getSelectedDmObjProp() {
        return selectedDmObjProp;

    }

    public void setSelectedDmObjProp(DmObjPropTable selectedDmObjProp) {
        this.selectedDmObjProp = selectedDmObjProp;
    }

    public ArrayList<DmObjPropTable> getDmObjPropList() {
        return dmObjPropList;
    }
//******************************
    private ArrayList<SelectItem> objectPropertiesList = new ArrayList<SelectItem>();
    private ObjectPropertiesDAO objectPropertiesDAO = new ObjectPropertiesDAO();
    public ArrayList<SelectItem> getObjectPropertiesList() {
        getAllObjectProperties();
        return objectPropertiesList;
    }

    public void setObjectPropertiesList(ArrayList<SelectItem> objectPropertiesList) {
        this.objectPropertiesList = objectPropertiesList;
    }

    public void getAllObjectProperties() {
        objectPropertiesList = new ArrayList<SelectItem>();
        ObjectPropertiesTable groupTable = new ObjectPropertiesTable();
        objectPropertiesList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itObjectProperties = objectPropertiesDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itObjectProperties.hasNext()) {
                groupTable = (ObjectPropertiesTable) itObjectProperties.next();
                objectPropertiesList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new ObjectPropertiesTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllObjectProperties() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    
    
    
    
    //******************************
    private ArrayList<SelectItem> dataModelObjectList = new ArrayList<SelectItem>();
    private DataModelObjectDAO dataModelObjectDAO = new DataModelObjectDAO();
    public ArrayList<SelectItem> getDataModelObjectList() {
        getAllDataModelObject();
        return dataModelObjectList;
    }

    public void setDataModelObjectList(ArrayList<SelectItem> dataModelObjectList) {
        this.dataModelObjectList = dataModelObjectList;
    }

    public void getAllDataModelObject() {
        dataModelObjectList = new ArrayList<SelectItem>();
        DataModelObjectTable groupTable = new DataModelObjectTable();
        dataModelObjectList.add(new SelectItem("", "- Select Role -"));
        try {
            Iterator itDataModelObject = dataModelObjectDAO.getByCriterionOrderedBy(null, null).iterator();
            while (itDataModelObject.hasNext()) {
                groupTable = (DataModelObjectTable) itDataModelObject.next();
                dataModelObjectList.add(new SelectItem(groupTable.getID(), groupTable.getCode()));
                groupTable = new DataModelObjectTable();
            }

        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in public void getAllDataModelObject() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }
    //******************************

    // *********** Actions ***********
    public void doFilter() {
        logger.log(Level.INFO, "Starting: public void doFilter () in DmObjPropBean");
        try {
            dmObjPropSrchCrit.setFilter(true);
            this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(dmObjPropSrchCrit, null);

            session.setAttribute("sessionDmObjPropSrchCrit", dmObjPropSrchCrit);
            fillDmObjPropList();
            logger.log(Level.INFO, "Finishing: public void doFilter () in DmObjPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception in doFilter() message: {0}Cause: {1}", new Object[]{e.getMessage(), e.getCause()});
        }
    }

        public void clearFilter(){
        try {
            dmObjPropSrchCrit = null;
            session.setAttribute("sessionDmObjPropSrchCrit",dmObjPropSrchCrit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sortByID() {

        logger.log(Level.INFO, "public void fillDmObjPropList ()");

        try {

            if (dmObjPropSrchCrit != null && colToOrderBy != null) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(dmObjPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmObjPropSrchCrit != null) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(dmObjPropSrchCrit, null);
            } else {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(null, null);
            }




        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmObjPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }

    public void update() {
        try {
            int parID = new Integer(session.getAttribute("dmObjPropIDSess").toString());
            dmObjPropUpdate.setID(parID);
            dmObjPropDAO.updateByID(dmObjPropUpdate);

        } catch (Exception e) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured interface the update action, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in Update(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void Insert() {
        logger.info("public void Insert()");
        try {

            int isInserted = -1;


            isInserted = dmObjPropDAO.insert(dmObjPropTable);

            if (isInserted != -1) {
                getContext().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Saved Successfully", "DmObjProp Added"));
                fillDmObjPropList();
            } else {
                context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "error", "error shit"));
                logger.log(Level.WARNING, "Exception occured in Insert(). Error Message: isInserted != -1 Probably exception occure in dmObjPropDAO.insert( dmObjPropTable )");

            }

        } catch (Exception e) {

            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure",
                    "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});

        }
    }

    public void delete() {
        try {

            int parID = new Integer(session.getAttribute("dmObjPropIDSess").toString());



            dmObjPropDAO.deleteByID(parID);
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "DmObjProp Deleted"));
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in delete(). Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Exception Occure", "Sorry!! an exception occured, the webmaster is notified and if action required it will be taken shortly"));
        }
    }

    public void setIDinSession() {
        try {
            Map<String, String> params = context.getExternalContext().getRequestParameterMap();
            int parID = new Integer(params.get("dmObjPropID"));
            dmObjPropUpdate = (DmObjPropTable) dmObjPropDAO.getByID(parID);
            session.setAttribute("dmObjPropUpdateSess", dmObjPropUpdate);
            session.setAttribute("dmObjPropIDSess", parID);
        } catch (Exception ex) {
            Logger.getLogger(DmObjPropBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void fillDmObjPropList() {
        logger.log(Level.INFO, "Starting: public void fillDmObjPropList () in DmObjPropBean");
        try {

            if ((dmObjPropSrchCrit != null && dmObjPropSrchCrit.isFilter()) && colToOrderBy != null) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(dmObjPropSrchCrit, colToOrderBy);
            } else if (colToOrderBy != null) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(null, colToOrderBy);
            } else if (dmObjPropSrchCrit != null && dmObjPropSrchCrit.isFilter()) {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(dmObjPropSrchCrit, null);
            } else {
                this.dmObjPropList = dmObjPropDAO.getByCriterionOrderedBy(null, null);
            }



            logger.log(Level.INFO, "finishing: public void fillDmObjPropList () in DmObjPropBean");
        } catch (Exception e) {
            logger.log(Level.WARNING, "Exception occured in public void fillDmObjPropList () . Error Message: {0}------- description:{1}", new Object[]{e.getMessage(), e.getStackTrace().toString()});
        }
    }
}

